//
//  TTRoomTabView.swift
//  TTProduct
//
//  Created by Toj on 3/18/21.
//  Copyright © 2021 TTProduct. All rights reserved.
//

import Cocoa

private let tabItemHeight: CGFloat = 34

protocol TTRoomTabViewDelegate : NSObjectProtocol {
    
    func tabView(_ tabView: TTRoomTabView, didTapTaber button: NSButton)
}

class TTRoomTabView: NSView {
    
    public weak var delegate: TTRoomTabViewDelegate?
    
    public var isDefault: Bool = true
    
    public var tabs: [String] = [] {
        didSet {
            disposeMakeFilterItems()
            
            if tabs.count == 0 { return }
            
            setupMakeTaberSubviews()
        }
    }
    
    private var buttons: [TTButton] = []
    
    private var selectedButton: TTButton?  {
        willSet {
            selectedButton?.tt_selected = false
            
            selectedButton?.tt_setTitleColor(.init(hexColor: "#4EBEFA"), for: .normal)
            selectedButton?.tt_setTitleColor(NSColor.init(hexColor: "#4EBEFA").tt_alpha(0.8), for: .hover)
            selectedButton?.tt_setTitleColor(NSColor.init(hexColor: "#4EBEFA").tt_alpha(0.8), for: .press)
            selectedButton?.tt_setBackgroundImage(nil, for: .normal)
            selectedButton?.tt_setBackgroundImage(nil, for: .hover)
            
            newValue?.tt_selected = true
            
            newValue?.tt_setTitleColor(.white, for: .normal)
            newValue?.tt_setTitleColor(nil, for: .hover)
            newValue?.tt_setTitleColor(nil, for: .press)
            newValue?.tt_setBackgroundImage(NSImage(named: "liveGoButtonNormal"), for: .normal)
            newValue?.tt_setBackgroundImage(NSImage(named: "liveGoButtonHover"), for: .hover)
        }
    }
}

// MARK: - override
extension TTRoomTabView {
    
    override func layout() {
        super.layout()
        
        setupMakeLayoutSubviews()
    }
}

// MARK: - 点击事件
extension TTRoomTabView {
    @objc private func didTapTaber(sender: TTButton) {
        if sender == selectedButton { return }
        selectedButton = sender
        
        delegate?.tabView(self, didTapTaber: sender)
    }
}

// MARK: - setup
extension TTRoomTabView {
    
    private func disposeMakeFilterItems() {
        buttons.forEach { (view) in
            view.removeFromSuperview()
        }
    }
    
    private func setupMakeTaberSubviews() {
        var index = 0
        for title in tabs {
            if title.count == 0 { continue }
            index += 1
            let view = setupFilterButtons(title:title.tt_localized)
            view.tag = index
            view.sizeToFit()
            addSubview(view)
            buttons.append(view)
        }
    }
    
    private func setupMakeLayoutSubviews() {
        let selfHeiht = frame.size.height
        let selfWidth = frame.size.width
        
        var x: CGFloat = 0
        let y: CGFloat = (selfHeiht - tabItemHeight) * 0.5
        let width: CGFloat = selfWidth / CGFloat(buttons.count)
        
        buttons.forEach { (view) in
            view.frame = CGRect(x: x, y: y, width: width, height: tabItemHeight)
            x += width
        }
        if (!isDefault) { return }
        selectedButton = buttons.first
    }
    
    private func setupFilterButtons(title: String) -> TTButton {
        let view = TTButton()
        view.tt_backgroundColor = .clear
        view.tt_titleFont = NSFont.systemFont(ofSize: 11)
        view.tt_setTitle(title, for: .normal)
        
        view.tt_setTitleColor(.init(hexColor: "#4EBEFA"), for: .normal)
        view.tt_setTitleColor(NSColor.init(hexColor: "#4EBEFA").tt_alpha(0.8), for: .hover)
        view.tt_setTitleColor(NSColor.init(hexColor: "#4EBEFA").tt_alpha(0.8), for: .press)
        
        view.tt_addTarget(self, action: #selector(self.didTapTaber(sender:)), for: .leftMouseUp)
        return view
    }
}
